// pages/address/address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '', //存放地区
    station: '', //存放维修站
    perList: '', // 存放维修人员
    multiArray: [
      [],
      [],
      []
    ],
    multiIndex: [0, 0, 0],
    customerid: null,
    name: '',
    phone: '',
    address: '',
    address1: '',
    changeaddress: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    this.changeaddress()
    // console.log(options.id)
    // var that = this
    // that.setData({
    //   customerid: options.id
    // })

    this.getCityStationPer();
  },
  getCityStationPer: function() {
    let that = this
    that.setData({
      city: app.globalData.city, //存放地区
      station: app.globalData.station, //存放维修站
      perList: app.globalData.perList // 存放维修人员
    })
    that.data.multiArray[0] = that.data.city
    that.data.multiArray[1] = this.getArr(that.data.city[0], that.data.station);
    that.data.multiArray[2] = this.getArr(that.data.multiArray[1][0], that.data.perList);
    that.setData({
      multiArray: that.data.multiArray
    })

  },
  /****列发生改变 */
  bindMultiPickerColumnChange: function(e) {
    let that = this
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        //第一列改变  设置第二列数据
        let arr = that.getArr(that.data.city[e.detail.value], that.data.station)
        data.multiArray[1] = arr
        that.setData({
          multiArray: data.multiArray
        })
        //从第二列中拿出第一项，设置第三组人员
        let arrColumn2 = that.getArr(arr[0], that.data.perList)
        data.multiArray[2] = arrColumn2
        that.setData({
          multiArray: data.multiArray
        })
        break;
      case 1:
        //第二列改变 设置第三列数据
        let arr2 = that.getArr(data.multiArray[1][e.detail.value], that.data.perList)
        data.multiArray[2] = arr2
        that.setData({
          multiArray: data.multiArray
        })
        break;
    }
  },

  getArr: function(value, arr) {
    for (let i in arr) {
      if (value == i) {
        return arr[i]
      }
    }
  },

  bindMultiPickerChange: function(e) {
    console.log(e.detail.value)
    var str = this.data.multiArray[0][e.detail.value[0]] + this.data.multiArray[1][e.detail.value[1]] + this.data.multiArray[2][e.detail.value[2]]
    this.setData({
      multiIndex: e.detail.value,
      address1: str
    })
  },


  // 获取修改后的input值
  changeText: function(e) {
    console.log(e)
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
    console.log(this.data[name])
  },
  //提交保存的用户信息
  formSubmit: function() {
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    console.log(this.data.customerid, this.data.name, this.data.address, this.data.phone)
    if (this.data.name == '') {
      wx.showModal({
        title: '提示',
        content: '请填写名字才能保存使用',
      })
    } else if (!myreg.test(this.data.phone)) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的电话才能保存使用',
      })
    } else if (this.data.address == '') {
      wx.showModal({
        title: '提示',
        content: '请填写地址才能保存使用',
      })
     }  else {
      var that = this
      app.req({
        url: 'User/CustomerWchat',
        method: "POST",
        data: {
          CustomerId: this.data.customerid,
          CustomerName: this.data.name,
          ComapnyAddress: this.data.address,
          Mb: this.data.phone,
          sex:this.data.sex
        },
        success: function(res) {
          console.log(res)
          if (res.data.Code == true) {
            wx.showToast({
              title: '修改成功',
              icon: 'success'
            })
          }
          that.onLoad()
          wx.navigateBack({
            
          })
        }
      })
    }
  },


  // 获取input的值
  changeaddress: function() {
    var that = this
    app.req({
      url: 'User/CustomerWchatDetail',
      success: function(res) {
        console.log(res)
        that.setData({
          changeaddress: res.data.Result,
          name: res.data.Result.CustomerName,
          phone: res.data.Result.Mb,
          address: res.data.Result.ComapnyAddress,
          customerid: res.data.Result.CustomerId
        })
      }
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})